//============================================================================
// Name        : file_send.cpp
// Author      : Jonathan Ellithorpe and Allen Lee
// Version     :
// Copyright   : UC Berkeley
// Description : Program that sends a file to a receiver using the GNUT Protocol
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <fcntl.h>
#include "ClientConnection.h"

using namespace std;

int main(int argc, char* argv[]) 
{
	// some variables 
	char* hostnameornumber;
	char* port;
	char* filename;
	int fd;

	// parse the arguments
	if( argc != 4 ){
		// panic! at the disco
		printf("Usage: hostname port filename\n");
		exit(1);
	} else
	{
		hostnameornumber = argv[1];
		port = argv[2];
		filename = argv[3];
		
	}

	// open the file for readingonly
	string path("./");
	path.append(filename);
	
	if( (fd = open(path.c_str(), O_RDONLY, 0)) == -1 ) {
		printf("ERROR -- Opening file in failed\n");
		exit(1);
	}
	
	ClientConnection CConn = ClientConnection(hostnameornumber, strlen(hostnameornumber), atoi(port));
	
	if( CConn.connect() == -1 )
		printf("Connect Failed\n");
	else if( CConn.sendFile(fd) == -1 )
		printf("sendFile failed\n");
	else if( CConn.closeConnection() == -1 )
		printf("closeConnection failed\n");
	
	
	CConn.closeSockets();
	
	close(fd);

	return EXIT_SUCCESS;
}
